﻿using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyRead.API.Appicant.Command;
using B.S.DataBase.MyRead.API.DTOs;
using MediatR;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler
{
    public class GetRolaNameCommandHandler : IRequestHandler<GetRolaNameCommand, APIResult<List<UserRoleDTO>>>
    {
        private readonly IBaseRepository<User> userRep;

        private readonly IBaseRepository<Role> roleRep;

        private readonly IBaseRepository<RoleUser> ruRep;

        public GetRolaNameCommandHandler(IBaseRepository<User> userRep, IBaseRepository<Role> roleRep, IBaseRepository<RoleUser> ruRep)
        {
            this.userRep = userRep;
            this.roleRep = roleRep;
            this.ruRep = ruRep;
        }
        public Task<APIResult<List<UserRoleDTO>>> Handle(GetRolaNameCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<UserRoleDTO>> result = new APIResult<List<UserRoleDTO>>();

            var list = userRep.GetAll()
                    .Join(ruRep.GetAll(), a => a.UserId, b => b.UserId, (a, b) => new { a, b })
                    .Join(roleRep.GetAll(), temp => temp.b.RoleId, o => o.RoleId, (temp, o) => new UserRoleDTO
                    {
                        UserId = temp.a.UserId,
                        UserName = temp.a.UserName,
                        DepId = temp.a.DepId,
                        NickName = temp.a.NickName,
                        Sex = temp.a.Sex,
                        ProvinceId = temp.a.ProvinceId,
                        CityId = temp.a.CityId,
                        CountyId = temp.a.CountyId,
                        HamletName = temp.a.HamletName,
                        RoleId = o.RoleId,
                        RoleName = o.RoleName,
                        RUId = temp.b.RUId,
                        CreateName = temp.a.CreateName,
                        CreateTime = temp.a.CreateTime,
                        UpdateName = temp.a.UpdateName,
                        UpdateTime = temp.a.UpdateTime,
                        IsDel = temp.a.IsDel,
                    }).GroupBy(x => x.UserId) // 按用户编号分组
                            .Select(group => new UserRoleDTO
                            {
                                UserId = group.Key,
                                UserName = group.First().UserName,
                                DepId = group.First().DepId,
                                NickName = group.First().NickName,
                                Sex = group.First().Sex,
                                ProvinceId = group.First().ProvinceId,
                                CityId = group.First().CityId,
                                CountyId = group.First().CountyId,
                                HamletName = group.First().HamletName,
                                CreateName = group.First().CreateName,
                                CreateTime = group.First().CreateTime,
                                UpdateName = group.First().UpdateName,
                                UpdateTime = group.First().UpdateTime,
                                RoleId = group.First().RoleId,
                                RoleName = string.Join(", ", group.Select(x => x.RoleName)), // 合并角色名称
                                RUId = group.First().RUId,
                                IsDel = group.First().IsDel
                            }).Where(x=>x.IsDel==false);
            result.Code = APIEnum.查询成功;
            result.Msg = APIEnum.查询成功.ToString();
            result.data = list.ToList();
            return Task.FromResult(result);
        }
    }
}
